#!/usr/bin/env python

# ------------------------------------------------------------
# ::                        WIGA-SOFT                       ::
# ------------------------------------------------------------
# personal.py
# Module for personal reports
# Created by: Wiktor`
# Version: 1.0
# ------------------------------------------------------------

from __future__ import division
from pao.models import Project, Employee, Task, Department, Budget

def personalReport(user):
    """
    Input: Username (user) 
    Output: Table (List of dictionaries) 
    [
      {'project': <Projekto pavadinimas>,
       'projectId: <Projekto kodas>,
       'manager': <Projekto vadovas>,
       'started': <Pradejo dalyvauti projekte>,
       'finished': <Baige dalyvauti projekte>,
       'workhours': <Isdirbo valandu>,
       'contribution': <Darbuotojo inasas i projekta procentais>}
      { .... }
      { .... }
    ]
    """

    allUserTasks = Task.objects.all()
    allUserTasks = allUserTasks.filter(employee__id__username = user)
    listOfProjects = []
    result = []
    for e in allUserTasks:
        # print str(e.id) + ' ' + str(e.project.id) + ' ' + str(e.employee.id) + ' ' + str(e.date) + ' ' + str(e.workhours)
        if e.project.id not in listOfProjects:
            listOfProjects.append(e.project.id)
    for e in listOfProjects:
        tmpTasks = allUserTasks.filter(project__id = e)
        allProjectTasks = Task.objects.all()
        allProjectTasks = allProjectTasks.filter(project__id = e)
        projectHours = 0
        for i in allProjectTasks:
            projectHours += i.workhours
        personalHours = 0
        tmpRez = {}
        attendance = []
        for i in tmpTasks:
            personalHours += i.workhours
            attendance.append(i.date)
        tmpRez['projectId'] = e
        tmpRez['project'] = Project.objects.get(id = e).name
        tmpRez['manager'] = Project.objects.get(id = e).manager.id.username
        tmpRez['started'] = min(attendance)
        """
        TODO: 
        it happens that person attends project only for a month,
        then 'started date' = 'finished date'.
        Should we make:
        'finished date' + 1 month
        Or
        'finished date' = 'finished date'.day(lastDayOfThisMonth())
        ?
        """
        tmpRez['finished'] = max(attendance) # +1 month
        tmpRez['workhours'] = personalHours
        tmpRez['contribution'] = personalHours * 100 / projectHours
        result.append(tmpRez)
    return result
    
def projectTasksReport(user, project):
    """
    Input: Username (user), ProjectID (project) 
    Output: Table (List of dictionaries) 
    [
        {'date': <Metai-Menuo>, 'workhours': <Isdirbo valandu>}
        { .... }
        { .... }
    ]
    """
    
    userTasks = Task.objects.filter(employee__id__username = user)
    userTasks = userTasks.filter(project__id = project)
    result = []
    for e in userTasks:
        # print str(e.id) + ' ' + str(e.project.id) + ' ' + str(e.employee.id) + ' ' + str(e.date) + ' ' + str(e.workhours)
        tmpRez = {}
        tmpRez['date'] = e.date
        tmpRez['name'] = e.name
        tmpRez['workhours'] = e.workhours
        result.append(tmpRez)
    return result
